//
//  SEGExpressCollectionListViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/1.
//

#import "SEGExpressCollectionListViewModel.h"
#import "SEGExpressRequestModel.h"

@interface SEGExpressCollectionListViewModel ()

@end

@implementation SEGExpressCollectionListViewModel
 

- (instancetype)init {
    self = [super init];
    if (self) {
        [self createSignal];
    }
    return self;
}
- (void)createSignal {
    @weakify(self);
    self.signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [[SEGExpressRequestModel sharedRequestModel] requestReceiveExpressId:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            NSDictionary *dict = result;
            NSString *code = [dict objectForKey:@"code"];
            if ([code isEqualToString:@"0"]) {
                [self.objectArray removeAllObjects];
                NSArray* data = [dict objectForKey:@"data"];
                for (int i = 0; i < data.count; i++) {
                    SEGExpressModel *model = [SEGExpressModel yy_modelWithDictionary:data[i]];
                    [self.objectArray addObject:model];
                }
            }
            
            [subscriber sendNext:self.objectArray];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

#pragma mark - Getter
- (NSMutableArray *)objectArray {
    if (!_objectArray) _objectArray = [NSMutableArray array];
    return _objectArray;
}


@end
